package com.atguigu.system.controller;


import com.atguigu.admin.model.entity.SysUser;
import com.atguigu.admin.service.SysUserService;
import com.atguigu.common.result.Result;
import com.atguigu.common.utils.JwtUtils;
import com.atguigu.system.model.entity.ProductInfoFlow;
import com.atguigu.system.model.entity.Spu;
import com.atguigu.system.service.ProductInfoFlowService;
import com.atguigu.system.service.SpuService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2025-03-01
 */
@RestController
@RequestMapping("/system/spu")
public class SpuController {

    @Autowired
    private SpuService bizSpuService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ProductInfoFlowService flowService;

    @PostMapping("/save")
    public  Result<?> saveSpu(@RequestBody Spu bizSpu,HttpServletRequest request) {
        bizSpuService.getBaseMapper().insert(bizSpu);
        ProductInfoFlow productInfoFlow = getProductInfoFlow(request,"SPU","新增", bizSpu.getId());
        flowService.save(productInfoFlow);
        return Result.ok();
    }

    @DeleteMapping("/remove/{id}")
    public  Result<?> removeSpuById(@PathVariable Integer id,HttpServletRequest request) {
        ProductInfoFlow productInfoFlow = getProductInfoFlow(request,"SPU","删除",id);
        flowService.save(productInfoFlow);
        return Result.ok(bizSpuService.removeSpuById(id));
    }

    @PutMapping("/update")
    public  Result<?> updateSpu(@RequestBody Spu bizSpu,HttpServletRequest request) {
        ProductInfoFlow productInfoFlow = getProductInfoFlow(request,"SPU","修改",bizSpu.getId());
        flowService.save(productInfoFlow);
        return Result.ok(bizSpuService.updateSpu(bizSpu));
    }

    @GetMapping("/get/{id}")
    public  Result<?> getSpuById(@PathVariable Integer id) {
        return Result.ok(bizSpuService.getSpuById(id));
    }

    @GetMapping("/listAll")
    public  Result<?> listAllSpus() {
        return Result.ok(bizSpuService.listAllSpus());
    }

    @GetMapping("/page")
    public Result<?> pageSkus(@RequestParam(defaultValue = "1") long current,
                               @RequestParam(defaultValue = "10") long size,
                               @RequestParam(required = false) String spuName) {
        Page<Spu> page = new Page<>(current, size);
        Page<Spu> spuPage = bizSpuService.pageSpusByName(page, spuName);
        return Result.ok(spuPage);
    }

    private ProductInfoFlow getProductInfoFlow(HttpServletRequest request, String ObjectType , String OperatorType,Integer id) {
        ProductInfoFlow productInfoFlow = new ProductInfoFlow();
        String userId = JwtUtils.getUserId(request.getHeader("token"));
        SysUser byId = sysUserService.getById(userId);
        productInfoFlow.setObjectType(ObjectType);
        productInfoFlow.setObjectId(id);
        productInfoFlow.setOperatorId(Math.toIntExact(byId.getId()));
        productInfoFlow.setOperatorName(byId.getName());
        productInfoFlow.setOperationType(OperatorType);
        return productInfoFlow;
    }


    /**
     * 获取级联下拉菜单
     * @return
     */
    @GetMapping("/cascader-options")
    public Result<?> getCascaderOptions() {
        return Result.ok(bizSpuService.getCascaderOptions());
    }
}

